package com.rayboo.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * 日期处理工具类
 * 
 * @author ray
 *
 */
public class DateUtil {

    private static String defaultDatePattern = null;
    private static Calendar cale = Calendar.getInstance();
    /**
     * 日期格式yyyy-MM-dd字符串常量
     */
    private static final String DATE_FORMAT = "yyyy-MM-dd";
    /**
     * 日期格式HH:mm:ss字符串常量
     */
    private static final String HOUR_FORMAT = "HH:mm:ss";
    /**
     * 日期格式yyyy-MM-dd HH:mm:ss字符串常量
     */
    private static final String DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";

    private static SimpleDateFormat sdf_date_format = new SimpleDateFormat(DATE_FORMAT);
    private static SimpleDateFormat sdf_hour_format = new SimpleDateFormat(HOUR_FORMAT);
    private static SimpleDateFormat sdf_datetime_format = new SimpleDateFormat(DATETIME_FORMAT);

    public static String now() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd");
        return sdf.format(new Date());
    }

    /**
     * 返回默认的日期格式
     *
     * @return
     */
    public static synchronized String getDatePattern() {
        defaultDatePattern = "yyyy-MM-dd";
        return defaultDatePattern;
    }

    /**
     * 获得服务器当前日期及时间，以格式为：yyyy-MM-dd HH:mm:ss的日期字符串形式返回
     *
     * @return
     */
    public static String getDateTime() {
        try {
            return sdf_datetime_format.format(cale.getTime());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获得服务器当前日期，以格式为：yyyy-MM-dd的日期字符串形式返回
     *
     * @return
     */
    public static String getDate() {
        try {
            return sdf_date_format.format(cale.getTime());
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获得服务器当前时间，以格式为：HH:mm:ss的日期字符串形式返回
     *
     * @return
     */
    public static String getTime() {
        String temp = " ";
        try {
            temp += sdf_hour_format.format(cale.getTime());
            return temp;
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 将指定日期按默认格式进行格式代化成字符串后输出如：yyyy-MM-dd
     *
     * @param aDate
     * @return
     */
    public static final String getDate(Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";
        if (aDate != null) {
            df = new SimpleDateFormat(getDatePattern());
            returnValue = df.format(aDate);
        }
        return (returnValue);
    }

    /**
     * 将日期类转换成指定格式的字符串形式
     *
     * @param aMask
     * @param aDate
     * @return
     */
    public static final String getDateTime(String aMask, Date aDate) {
        SimpleDateFormat df = null;
        String returnValue = "";

        if (aDate == null) {
            return null;
        } else {
            df = new SimpleDateFormat(aMask);
            returnValue = df.format(aDate);
        }
        return (returnValue);
    }

    /**
     * 将日期字符串按指定格式转换成日期类型
     *
     * @param aMask   指定的日期格式，如:yyyy-MM-dd
     * @param strDate 待转换的日期字符串
     * @return
     * @throws ParseException
     */
    public static final Date convertStringToDate(String aMask, String strDate)
            throws ParseException {
        SimpleDateFormat df = null;
        Date date = null;
        df = new SimpleDateFormat(aMask);

        try {
            date = df.parse(strDate);
        } catch (ParseException pe) {
            throw pe;
        }
        return (date);
    }

    /**
     * 将日期字符串按默认格式转换成日期类型
     *
     * @param strDate
     * @return
     * @throws ParseException
     */
    public static Date convertStringToDate(String strDate)
            throws ParseException {
        Date aDate = null;
        try {
            aDate = convertStringToDate(getDatePattern(), strDate);
        } catch (ParseException pe) {
            throw new ParseException(pe.getMessage(), pe.getErrorOffset());
        }
        return aDate;
    }

    /**
     * 获取明天的日期
     *
     * @return
     */
    public static String getNextDate() {
        String nextDate = null;
        try {
            //获取当前日期
            Date date = new Date();
            SimpleDateFormat sf = new SimpleDateFormat(getDatePattern());
            String nowDate = sf.format(date);
            Calendar cal = Calendar.getInstance();
            cal.setTime(sf.parse(nowDate));
            cal.add(Calendar.DAY_OF_YEAR, +1);
            nextDate = sf.format(cal.getTime());
        } catch (Exception e) {
            e.getStackTrace();
        }
        return nextDate;
    }

    public static String getStartOfWeek() {
        String startOfWeek = null;
        SimpleDateFormat sf = new SimpleDateFormat(getDatePattern());
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
//        cal.setFirstDayOfWeek(Calendar.MONDAY);
//        cal.setTime(new Date());
//        cal.set(Calendar.DAY_OF_WEEK, cal.getFirstDayOfWeek());
        startOfWeek = sf.format(cal.getTime());
        return startOfWeek;
    }

    public static String getLastOfWeek() {
        String lastOfWeek = null;
        SimpleDateFormat sf = new SimpleDateFormat(getDatePattern());
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        cal.set(Calendar.WEEK_OF_YEAR, 1);
        lastOfWeek = sf.format(cal.getTime());
        return lastOfWeek;
    }

    public static String getStartOfWeekend() {
        String startOfWeekend = null;
        SimpleDateFormat sf = new SimpleDateFormat(getDatePattern());
        Calendar cal = Calendar.getInstance();
        cal.set(Calendar.DAY_OF_WEEK, Calendar.SATURDAY);
        startOfWeekend = sf.format(cal.getTime());
        return startOfWeekend;
    }

    /*public static void main(String[] args) {
        getNextDate();
        getStartOfWeek();
        getLastOfWeek();
        getStartOfWeekend();
    }*/
}
